<script setup>
import {reactive, ref} from "vue";
import request from "@/utils/request.js";
import {ElMessage} from "element-plus";
import router from "@/router/index.js";

const data = reactive({
  form: {},
  rules: {
    username: [
      { required: true, message: '请输入账号', trigger: 'blur' },
    ],
    password: [
      { required: true, message: '请输入密码', trigger: 'blur' },
    ],
  }
});

const formRef = ref();

const login = () => {
  formRef.value.validate((valid) => {
    if (valid) {
      request.post('/login',data.form).then(res => {
        if (res.code === '200'){
          localStorage.setItem('code_user',JSON.stringify(res.data || {}))
          ElMessage.success("登录成功")
          router.push('/home')
        }else {
          ElMessage.error(res.msg)
        }
      })
    }
  })
}

</script>

<template>
  <div>
    <!-- 原有登录页面内容包裹在 v-show 中 -->
    <div class="login">
      <div style="width: 100%;display: flex;flex-direction: column;justify-self: center">
        <span style="margin-left: 15px;font-size: 30px;font-weight: bold">登录</span>
        <span style="margin-left: 15px;margin-top: 10px;font-size: 18px;color: rgba(0, 0, 0, 0.35)">请将您的账号输入登录</span>
      </div>
      <div style="margin-top: 50px;padding: 0 15px">
        <el-form ref="formRef" :model="data.form" :rules="data.rules" >
          <el-form-item prop="phone">
            <el-input size="large" v-model="data.form.username" autocomplete="off"
                      prefix-icon="Phone" placeholder="请输入账号" style="height: 50px"
                      class="loginInput"
            />
          </el-form-item>
          <el-form-item prop="password">
            <el-input size="large" v-model="data.form.password" show-password autocomplete="off" prefix-icon="Lock" placeholder="请输入密码" style="height: 50px" class="loginInput" />
          </el-form-item>
        </el-form>
        <div style="margin-top: 30px">
          <el-button size="large" type="primary"
                     style="width: 100%;margin-bottom: 10px;border-radius: 10px;height: 60px;font-size: 20px" @click="login">登 录</el-button>
        </div>
        <div style="margin-top: 10px;display: flex">
          <div style="margin-left: auto">
            还没有账号？请<a style="color: #0266ff" href="/register">注 册</a>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
/* 新增：闪屏层样式 */
.splash-screen {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: white;
  z-index: 9999; /* 确保在最顶层 */
}

.splash-image {
  max-width: 80%;
  max-height: 80%;
  object-fit: contain;
}

.login{
  display: flex;
  justify-content: center;
  flex-direction: column;
  height: 100vh;
  overflow: hidden;
  background-image: url("@/assets/img/login-bg.jpg");
  background-size: cover;
  padding: 10px;
}
</style>